package com.object.oriented.exam.system4.shoudongsystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ExamBuilder {

    public static void main(String[] args) {
        ExamBuilder examBuilder = new ExamBuilder();
        examBuilder.buildExam();
    }

    private static final String DB_URL = "jdbc:mysql://8.134.110.25:3306/exam?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT";
    private static final String DB_USERNAME = "exam";
    private static final String DB_PASSWORD = "dfhi3AZBSDMWsJwr";

    private HashMap<Integer, Question> questionBank;
    private ArrayList<Question> selectedQuestions;
    private HashMap<Question, Integer> scoreMap;

    int k=0;

    public ExamBuilder() {
        questionBank = new HashMap<>();
        selectedQuestions = new ArrayList<>();
        scoreMap = new HashMap<>();

        // 从数据库加载问题库
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try (Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
                 Statement statement = connection.createStatement()) {

                ResultSet resultSet = statement.executeQuery("SELECT * FROM question_bank");
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    k=k+1;
                    String description = resultSet.getString("description");
                    String answer = resultSet.getString("answer");

                    questionBank.put(id, new Question(id, description, answer));
               }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void buildExam() {
        Scanner input = new Scanner(System.in);


        //显示题库中所有题目
        System.out.println("以下是题库中所有题目：");
        for (Map.Entry<Integer, Question> entry : questionBank.entrySet()) {
            Integer key = entry.getKey();
            Question value = entry.getValue();
            System.out.println(value.toString());
            //"Key: " + key + ", Value: " +
        }
        // 手动选择问题并设置分值
        while (true) {
            System.out.println("请选择题库中题目的编号（0表示完成组卷）：");
            int qid = input.nextInt();
            if (qid < 1 || !questionBank.containsKey(qid)) {
                break;
            }
            System.out.println("请输入该题目的分值：");
            int score = input.nextInt();
            Question question = questionBank.get(qid);
            if (question != null) {
                selectedQuestions.add(question);
                scoreMap.put(question, score);
            }
        }
        input.close();

        // 显示所选问题及其分数，并计算总分
        System.out.println("您选择了以下题目和分数：");
        int totalScore = 0;
        for (Question question : selectedQuestions) {
            int score = scoreMap.getOrDefault(question, 0);
            System.out.println(question.getDescription() + "（" + score + "分）");
            totalScore += score;
        }
        System.out.println();
        System.out.println("试卷总分为：" + totalScore + "分");
    }
}

class Question {
    private int id;
    private String description;
    private String answer;

    public Question(int id, String description) {
        this(id, description, null);
    }

    public Question(int id, String description, String answer) {
        this.id = id;
        this.description = description;
        this.answer = answer;
    }

    public int getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public String getAnswer() {
        return answer;
    }
    @Override
    public String toString() {
        return  id +"."+
                description ;
    }
}